package com.sdry.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.sdry.entity.GoDownDetail;
import com.sdry.entity.GodownEntry;
import com.sdry.entity.GodownEntryExample;
import com.sdry.entity.PartOrder;
import com.sdry.entity.PartOrderExample;
import com.sdry.entity.UpmsSupplier;
import com.sdry.entity.UpmsSupplierExample;
import com.sdry.service.GoDownDetailService;
import com.sdry.service.GoDownEntryService;
import com.sdry.service.PartOrderService;
import com.sdry.service.gemini.UpmsSupplierService;
import com.sdry.utils.RequestResult;
import com.sdry.utils.TableResult;

/**
  * @ClassName: GodownEntryController
  * @Description: 入库
  * @author cb
  * @date 2018/7/27 11:01
  */
@Controller
@RequestMapping("/godown")
public class GodownEntryController {

    @Resource
    private GoDownEntryService godownEntryService;

    
    @Resource
    private GoDownDetailService goDownDetailService;
    
    
    @Autowired
	private PartOrderService partOrderService;
    
    @Autowired
    private UpmsSupplierService upmsSupplierService;

    /**
     * 跳转页面
     * @return
     */
    @RequestMapping("/pageGo")
    public String pageGo() {

        return "goDownEntry/list";
    }

    @RequestMapping("/pageAdd")
    public String pageAdd() {


        return "/goDownEntry/addEdit";
    }

    /**
     * 入库添加
     * @param godownEntry 对象
     */
    @RequestMapping("/insertGoDownEntry")
    @ResponseBody
    public Map<String,Object> insertGoDownEntry(GodownEntry godownEntry,@DateTimeFormat(pattern = "yyyy-MM-dd")Date time) {

    	godownEntry.setGotime(time);
    	
        long effect = godownEntryService.insertSelective(godownEntry);

        Map<String, Object> map = RequestResult.requestResultLong(effect);

        return map;
    }

    
    /**
     * 分页查询
     * @param limit 大小
     * @param offset 当前页
     */
    @RequestMapping("/querySysOpListPageHelper")
    @ResponseBody
    public TableResult querySysOpListPageHelper(String departNo ,@RequestParam(value="limit",required=false)String limit,@RequestParam(value="offset",required=false)String offset) {
    	
    	
    	PageHelper.startPage(Integer.parseInt(offset), Integer.parseInt(limit));
    	
    	GodownEntryExample example = new GodownEntryExample();
    	
    	if (StringUtil.isNotEmpty(departNo)) {
    		example.createCriteria().andCremark1Like(departNo);
		}
    	
		List<GodownEntry> list = godownEntryService.selectByExample(example);
    	
    	PageInfo<GodownEntry> info = new PageInfo<GodownEntry>(list);
    	
    	TableResult tableResult = new TableResult(info.getTotal(), info.getList()); 
    	
		return tableResult;
    	
    }
    
    /**
     * 删除
     * @param ids id数组
     * @return
     */
    @RequestMapping("/deleteGoDown")
    @ResponseBody
    public Map<String,Object> deleteGoDown(String ids) {
    	
    	int effect = 0;
    	
    	String[] split = ids.split(",");
    	
    	for (String str : split) {
    		effect = godownEntryService.delete(str);
		}
    	
    	
    	Map<String, Object> map = RequestResult.requestResultInt(effect);
    	
    	return map;
    }

    /**
     * 查询所有的入库单
     * @return
     */
    @RequestMapping("/phone/selectAll")
    @ResponseBody
    public Map<String,Object> selectAll() {
    	GodownEntryExample example = new GodownEntryExample();
		List<GodownEntry> selectByExample = godownEntryService.selectByExample(example);
		return RequestResult.responsePhone(1,selectByExample);
    }
    
    /**
     * 手持端入库操作
     * @param conde  条码
     * @param infoId 备品备件ID
     * @param positionId 库位ID
     * @param godownid 入库单ID
     */
   @RequestMapping("/phone/insertDetail")
    @ResponseBody
    public Map<String,Object> insertDetail(String conde,String infoId,String positionId,String godownid) {
    	
    	GoDownDetail record = new GoDownDetail();
    	
    	record.setConde(conde);
    	record.setInfoid(Long.parseLong(infoId));
    	record.setGremark2(positionId);
    	record.setWgodownid(Long.parseLong(godownid));
    	
    	int insert = goDownDetailService.insertSelective(record);
    	
		return RequestResult.responsePhone(insert,"");
    }
   
   /**
    * 申请单
    * @return
    */
   @RequestMapping("/listOrder")
   @ResponseBody
   public List<PartOrder> listOrder() {
	   PartOrderExample example = new PartOrderExample();
	   List<PartOrder> list = partOrderService.selectByExample(example);
	   
	   return list;
   }
   
   /**
    * 供应商
    * @return
    */
   @RequestMapping("/selectSupper")
   @ResponseBody
   public List<UpmsSupplier> selectSupper() {
	   
		UpmsSupplierExample example = new UpmsSupplierExample();
		List<UpmsSupplier> list = upmsSupplierService.selectByExample(example);
		
		return list;
   }
   
   /**
    * 查找详细
    * @param cremark1 编号
    * @return
    */
   @RequestMapping("/listDetailed")
   @ResponseBody
   public TableResult listDetailed(String cremark1) {
	   
	   
	   List<GoDownDetail> list = goDownDetailService.selectByCremark1(cremark1);
	   
	   TableResult tableResult = new TableResult(null, list); 
   	
   	
		return tableResult;
   }
}
